package com.lefull.lefullbase.utils.OkGoHelper;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okgo.request.PostRequest;
import com.lzy.okgo.request.base.Request;

import org.apache.http.HttpException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Map;

/**
 * 连接OkGo请求方法类
 */
public class OkGoNetHelper {
    public static int NO_NETWORK = -1;
    public static int OUT_TIME_NETWORK = -2;
    public static int NO_NET_SERVICE = -3;

    OkGoNetHelper() {
    }

    /**
     * @param object            上下文，用于标记网络请求，用于取消网络请求，缓存也需要
     * @param params            请求参数
     * @param url               已拼接完整的url
     * @param isCache           是否开启缓存
     * @param leFullNetCallback 请求方法回调，告知调用者
     * @funcation 发起get请求
     */
    public static void getRequest(Object object, Map<String, String> header, Map<String, String> params, String url, Boolean isCache, LeFullNetCallback leFullNetCallback) {
        //判断该请求是否缓存
        if (isCache) {
        } else {
            Request<String, GetRequest<String>> request = OkGo.<String>get(url);
            OkGoNetHelper.request(request, object, header, params, leFullNetCallback);

        }
    }

    /**
     * @param object            上下文，用于标记网络请求，用于取消网络请求，缓存也需要
     * @param params            请求参数
     * @param url               已拼接完整的url
     * @param isCache           是否开启缓存
     * @param leFullNetCallback 请求方法回调，告知调用者
     * @funcation 发起post请求
     */
    public static void postRequest(Object object, Map<String, String> header, Map<String, String> params, String url, Boolean isCache, LeFullNetCallback leFullNetCallback) {
        //判断该请求是否缓存
        if (isCache) {
        } else {
            Request<String, PostRequest<String>> request = OkGo.<String>post(url);
            OkGoNetHelper.request(request, object, header, params, leFullNetCallback);
        }
    }

    private static void request(Request request, Object object, final Map<String, String> header, Map<String, String> params, final LeFullNetCallback leFullNetCallback) {
        request.tag(object)
                .params(params)
                .execute(new LeFullStringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        //拼接header
                        HttpHeaders headers = new HttpHeaders();
                        for (Map.Entry<String, String> entry : header.entrySet()) {
                            headers.put(entry.getKey(), entry.getValue());
                        }
                        request.headers(headers);
                        leFullNetCallback.leFullNetworkStart();
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        //请求成功，通过leFullNetCallback返回String类型的response
                        leFullNetCallback.leFullNetworkSuccess(response.body());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        //请求失败，通过leFullNetCallback返回String类型的response(错误信息)
                        Throwable exception = response.getException();
                        if (exception != null) exception.printStackTrace();
                        if (exception instanceof UnknownHostException || exception instanceof ConnectException) {
                            leFullNetCallback.leFullNetworkError(NO_NETWORK, "网络连接失败，请连接网络");
                        } else if (exception instanceof SocketTimeoutException) {
                            leFullNetCallback.leFullNetworkError(OUT_TIME_NETWORK, "网络请求超时");
                        } else if (exception instanceof HttpException) {
                            leFullNetCallback.leFullNetworkError(NO_NET_SERVICE, "服务器响应码404和500");
                        }

                    }
                });
    }

}
